package libaryinterface;

import jdbc.DeleteJdbc;
import jdbc.SearchJdbc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-14
 * Time: 21:14
 * author:SearchInterface
 */
public class SearchInterface extends JFrame{
    public ResultSet result;
    public SearchInterface() {
        //设置窗口
        super("查找界面");

        //创建书名标签
        JLabel label = new JLabel("书名:");
        label.setBounds(200, 150, 100, 40);

        //设置标签字体
        Font font = new Font("黑体", Font.BOLD, 20);
        label.setFont(font);

        //创建文本框
        JTextField textField = new JTextField();
        textField.setBounds(300, 150, 300, 40);
        textField.setFont(font);

        //创建查找按钮
        JButton button = new JButton("查找");
        button.setBounds(400, 200, 80, 30);
        button.setFont(font);
        //设置监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = textField.getText();
                try {
                   SearchJdbc searchJdbc = new SearchJdbc();
                   result = searchJdbc.search (name);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                //弹窗
                try {
                    if( !result.next() ){
                        new MyDialogDemo("要查找的书籍不存在!");
                    } else {
                        try {
                            new ShowInterface(result);
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        //获得容器
        Container container = this.getContentPane();
        container.setBackground(new Color(50, 144, 236));
        setBounds(200, 200, 800, 600);
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //添加
        container.add(label);
        container.add(textField);
        container.add(button);
    }
}
